<template>
<div desc="">
	<span v-if="abPermission=='r'" class="readonly-text">{{ desc }}</span>
	<span :class="cls" v-else>
		<!-- 下拉单选/下拉多选 -->
		<div v-if="type == 'proSelectRadio' || type == 'proSelectCheckbox'">
			<gz-route-prop v-model="data" :datalist="[treeData]" :type="type" @handleChange="treeChange"></gz-route-prop>
		</div>
		<!-- 数字范围 -->
		<div v-if="type == 'inputRange'">
			<input class="weui-input" v-model="data" type="number" @blur="treeChange" :disabled="disabled" :placeholder="placeholder">
		</div>
	</span>
</div>
</template>

<script>
/**
 * 修改后目前只有下拉单选和下拉多选两种方式（2024-2-28）
 * 产品属性 checkbox多选模式
 * propropertyid   是字典的id
 * baseproductid   产品id
 * <gz-product-dispose v-model="data.incomingBusinessObj.WF_LOAN_APPLICATION.LPR" v-bind:ab-permission="permission.incomingBusinessObj.WF_LOAN_APPLICATION.LPR" type="proSelectRadio" propropertyid="443473547132665889" :baseproductid="data.incomingBusinessObj.PROD_ID ? data.incomingBusinessObj.PROD_ID : data.incomingBusinessObj.PROD_ID = $route.query.prodId" desc="13贷款申请信息-利率" v-ab-validate="{&quot;maxlength&quot;:50}"></gz-product-dispose>
 * <gz-product-dispose v-model="data.zlxxywdx.BANK_NETWORK2" placeholder="Enter something..." type="proSelectCheckbox" propropertyid="443473547132665865" baseproductid="447855500736331777">
 * <gz-product-dispose v-model="data.kjcs.sjzd" placeholder="Enter something..." type='proInputCheckbox' >​</gz-product-dispose>
 * 
 */
import Vue from 'vue'
import gzRouteProp from './gz-route-prop.vue';
export default {
  components: {
	gzRouteProp
  },
  props: ["value","placeholder","abPermission","readonlyShowText","disabled","type", "baseproductid", "propropertyid"],
  data () {
      return {
		data: this.type == 'proSelectRadio' || this.type == 'proSelectCheckbox' ? (this.value ? this.value.split(",") : []) : Number(this.value),
		treeData: [],
		desc : "",
		cls : "",
		// ltSignal: "", //ltSignal: [{code: "MORE", name: ">"}, {code: "MORE_EQUAL", name: ">="}] 起始值符号
		// ltVal: -Infinity, //起始值
		// rgSignal: "", //rgSignal: [{code: "LESS", name: "<"}, {code: "LESS_EQUAL", name: "<="}] 终止值符号
		// rgVal: Infinity //终止值
      }
  },
  mounted: function () {
	  	// if(this.value){
		// 	if(this.type =='proSelectRadio'){
		// 		this.data = this.value;
		// 	}else if (this.type =='inputRange'){
		// 		this.data = Number(this.value);
		// 	}else{
		// 		this.data = this.value.split(",");
		// 	}
		// }
	  	this.loadDicData();
  },
  methods: {
	  treeChange : function(value){
		if(this.type =='inputRange'){
			// ltSignal: "", //ltSignal: [{code: "MORE", name: ">"}, {code: "MORE_EQUAL", name: ">="}] 起始值符号
			// 	ltVal: NaN, //起始值
			// 	rgSignal: "", //rgSignal: [{code: "LESS", name: "<"}, {code: "LESS_EQUAL", name: "<="}] 终止值符号
			// 	rgVal: NaN //终止值
			if(this.ltSignal && this.ltSignal != '' && this.ltVal && this.ltVal !='' && this.ltVal !=NaN && this.ltVal != -Infinity){
				if(this.ltSignal == 'MORE'){
					if(this.data < this.ltVal || this.data == this.ltVal){
						this.$vnode.context.$validity[this.$attrs.desc] = "请输入大于"+this.ltVal+"的数";
						this.cls = "ivu-form-item-error";
					}else{
						if(this.rgSignal && this.rgSignal != '' && this.rgVal && this.rgVal !='' && this.rgVal !=NaN && this.rgVal != Infinity){
							if(this.rgSignal == 'LESS'){
								if(this.data > this.rgVal || this.data == this.rgVal){
									this.$vnode.context.$validity[this.$attrs.desc] = "请输入小于"+this.rgVal+"的数";
									this.cls = "ivu-form-item-error";
								}else{
									delete this.$vnode.context.$validity[this.$attrs.desc];
									this.cls = "";
									this.$emit("input", this.data);
								}
							}else{
								if(this.data > this.rgVal){
									this.$vnode.context.$validity[this.$attrs.desc] = "请输入小于或等于"+this.rgVal+"的数";
									this.cls = "ivu-form-item-error";
								}else{
									delete this.$vnode.context.$validity[this.$attrs.desc];
									this.cls = "";
									this.$emit("input", this.data);
								}
							}
						}else{
							delete this.$vnode.context.$validity[this.$attrs.desc];
							this.cls = "";
							this.$emit("input", this.data);
						}
						
					}
				}else{
					if(this.data < this.ltVal){
						this.$vnode.context.$validity[this.$attrs.desc] = "请输入大于或等于"+this.ltVal+"的数";
						this.cls = "ivu-form-item-error";
					}else{
						if(this.rgSignal && this.rgSignal != '' && this.rgVal && this.rgVal !='' && this.rgVal !=NaN && this.rgVal != Infinity){
							if(this.rgSignal == 'LESS'){
								if(this.data > this.rgVal || this.data == this.rgVal){
									this.$vnode.context.$validity[this.$attrs.desc] = "请输入小于"+this.rgVal+"的数";
									this.cls = "ivu-form-item-error";
								}else{
									delete this.$vnode.context.$validity[this.$attrs.desc];
									this.cls = "";
									this.$emit("input", this.data);
								}
							}else{
								if(this.data > this.rgVal){
									this.$vnode.context.$validity[this.$attrs.desc] = "请输入小于或等于"+this.rgVal+"的数";
									this.cls = "ivu-form-item-error";
								}else{
									delete this.$vnode.context.$validity[this.$attrs.desc];
									this.cls = "";
									this.$emit("input", this.data);
								}
							}
						}else{
							delete this.$vnode.context.$validity[this.$attrs.desc];
							this.cls = "";
							this.$emit("input", this.data);
						}
					}
				}
			}else if(this.rgSignal && this.rgSignal != '' && this.rgVal && this.rgVal !='' && this.rgVal !=NaN && this.rgVal != Infinity){
				if(this.rgSignal == 'LESS'){
					if(this.data > this.rgVal || this.data == this.rgVal){
						this.$vnode.context.$validity[this.$attrs.desc] = "请输入小于"+this.rgVal+"的数";
						this.cls = "ivu-form-item-error";
					}else{
						delete this.$vnode.context.$validity[this.$attrs.desc];
						this.cls = "";
						this.$emit("input", this.data);
					}
				}else{
					if(this.data > this.rgVal){
						this.$vnode.context.$validity[this.$attrs.desc] = "请输入小于或等于"+this.rgVal+"的数";
						this.cls = "ivu-form-item-error";
					}else{
						delete this.$vnode.context.$validity[this.$attrs.desc];
						this.cls = "";
						this.$emit("input", this.data);
					}
				}
			}
		}else if(this.type =='proSelectRadio'){
			this.$emit("input", value.join(" "));
		}else{
			this.$emit("input", value.join(","));
		}
	  },
	  loadDicData : function(){
			var vm = this;
			let url = this.__ctx+"/product/prodProduct/queryProdPropDict";
	 		let post = this.baseService.postForm(url,{prodId: this.baseproductid, prodDictId: this.propropertyid});
	 		this.abTools.getResultData(post, data => {
				// if(vm.type =='inputRange'){
				// 	vm.ltSignal= data.ltSignal; // 起始值符号
				// 	vm.ltVal= Number(data.ltVal); //起始值
				// 	vm.rgSignal= data.rgSignal; // 终止值符号
				// 	vm.rgVal= Number(data.rgVal); //终止值
				// }else{
					if (data.length > 0) {
						let dataList = data
						for(var i=0;i<dataList.length;i++){
							dataList[i].value = dataList[i].key;
						}
						vm.treeData = dataList;
					}
				// }
	 		});
		},
		valueChange : function(){
		  if(!this.$vnode.context.$validity){
			  this.$vnode.context.$validity = {};
		  }

		 if(this.abPermission == "b"){
			  if(!this.value){
				  this.$vnode.context.$validity[this.$attrs.desc] = "必填";
				  this.cls = "ivu-form-item-error";
			  }else{
				  delete this.$vnode.context.$validity[this.$attrs.desc];
				  this.cls = "";
			  }
		  }else if(this.$vnode.context.$validity[this.$attrs.desc]){
		  	delete this.$vnode.context.$validity[this.$attrs.desc];
		  	this.cls = "";
		  }


		  if(!this.value){
			  this.desc = "";
			  return;
		  }
		  var names = [];
		  if(this.type !='inputRange'){
			var valueStr = this.value +'';
			valueStr.split(",").forEach(item =>{
				if(!this.treeMap[item]) return;
				names.push(this.treeMap[item].name);
				if(this.type =='proCheckbox' || this.type =='proSelectCheckbox' || this.type =='proInputCheckbox'){
						this.treeMap[item].checked = true;
					}else{
						this.treeMap[item].selected = true;
					}
			});
		  }
		  this.desc = names.join(",");
		  if(this.type =='proRadio' || this.type =='proSelectRadio' || this.type =='proInputRadio'){
				this.data = this.value;
			}else if(this.type =='inputRange'){
				this.data = Number(this.value);
			}else{
				this.data = this.value.split(",");
			}
	  },
  },
  watch : {
	//   abPermission: function(){
	// 	  this.valueChange();
	//   },
	//   value : function(){
	// 	  this.valueChange();
	//   }
  }
}
</script>
<style scoped lang="less">
/deep/.vux-no-group-title {
    margin-top: 0;
}
</style>
